package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.core.JsonResult;
import com.wuming.caiwu.model.Member;
import com.wuming.caiwu.model.SysUser;
import com.wuming.caiwu.model.SysUserDetail;
import com.wuming.caiwu.model.SysUserRole;
import com.wuming.caiwu.service.MemberService;
import com.wuming.caiwu.service.SysUserRoleService;
import com.wuming.caiwu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/sysuser")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @RequestMapping(value = "/manageSysUser", method = RequestMethod.GET)
    public String manageSysUser() {
        return "/sysuser/manageSysUser";
    }

    @RequestMapping(value = "/addSysUser", method = RequestMethod.GET)
    public String addSysUser() {
        return "/sysuser/editSysUser";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) String username,@RequestParam(required = false) Long qsgStoreId,
                             @RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            List<SysUser> users = sysUserService.getSysUsersByPage(username,qsgStoreId,pageIndex,pageSize);
            int total = sysUserService.getTotal(username,qsgStoreId);
            return CommonResult.createSuccessResult(users, total, "查询用户信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询用户信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询用户信息失败.");
        }
    }

    @RequestMapping(value = "/editSysUser/{id}", method = RequestMethod.GET)
    public String editSysUser(Model model, @PathVariable Long id) {
        logger.info("准备修改用户信息ID:{}", id);
        SysUserDetail user = sysUserService.getUserDetailById(id);
        model.addAttribute("user", user);
        SysUserRole userRole = sysUserRoleService.getUserRoleByUserId(id);
        if(null != userRole){
            model.addAttribute("currentRoleId", userRole.getRoleId());
        }else{
            model.addAttribute("currentRoleId",null);
        }
        return "/sysuser/editSysUser";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(SysUser user) {
        try {
            if (null != user.getId()) {
                sysUserService.modifySysUser(user);
            } else {
                sysUserService.saveSysUser(user);
            }
        } catch (Exception e) {
            logger.error("保存用户信息失败,失败信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存用户基本信息失败", false);
        }
        return new JsonResult<Object>(user.getId(), "保存用户基本信息成功.", true);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Long id) {
        logger.info("准备删除用户信息ID:{}", id);
        try {
            sysUserService.removeSysUserById(id);
            return CommonResult.createSuccessResult("删除用户信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除用户信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除用户信息失败.");
        }
    }

}
